package cqrtplm.controller;

import com.github.pagehelper.PageInfo;
import com.hustcad.plm.pdm.file.handler.response.ResponseResult;
import cqrtplm.constant.RouteConstant;
import cqrtplm.dto.RtCfgBomConfigurationDTO;
import cqrtplm.entity.RtCfgBomConfigurationDO;
import cqrtplm.service.RtCfgBomConfigurationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(tags = "配置管理-需求状态表-零部件装配bom更改规则表")
@RequestMapping(RouteConstant.RTCFGBOMCONFIGURATION + "v1")
public class RtCfgBomConfigurationController {
    @Resource
    private RtCfgBomConfigurationService configurationService;

    /**
     * 根据主键查询
     */
    @ApiOperation("根据主键查询配置信息")
    @GetMapping("/{oid}")
    public ResponseResult<RtCfgBomConfigurationDO> selectByPrimaryKey(@PathVariable Long oid) {
        RtCfgBomConfigurationDO configuration = configurationService.selectByPrimaryKey(oid);
        if (configuration != null) {
            return ResponseResult.success(configuration,"");
        } else {
            return ResponseResult.fail("","404", "未找到对应配置信息");
        }
    }

    /**
     * 查询所有记录
     */
    @ApiOperation("查询所有配置信息")
    @GetMapping("/list")
    public ResponseResult<List<RtCfgBomConfigurationDO>> selectAll() {
        List<RtCfgBomConfigurationDO> list = configurationService.selectAll();
        return ResponseResult.success(list,"");
    }

    /**
     * 根据条件查询
     */
    @ApiOperation("根据条件查询配置信息")
    @PostMapping("/list/condition")
    public ResponseResult<List<RtCfgBomConfigurationDO>> selectByCondition(@RequestBody RtCfgBomConfigurationDTO configurationDTO) {
        List<RtCfgBomConfigurationDO> list = configurationService.selectByCondition(configurationDTO);
        return ResponseResult.success(list,"");
    }

    /**
     * 根据条件分页查询
     */
    @ApiOperation("根据条件分页查询配置信息")
    @PostMapping("/list/page")
    public ResponseResult<PageInfo<RtCfgBomConfigurationDO>> selectByConditionWithPage(@RequestBody RtCfgBomConfigurationDTO configurationDTO) {
        PageInfo<RtCfgBomConfigurationDO> pageInfo = configurationService.selectByConditionWithPage(configurationDTO);
        return ResponseResult.success(pageInfo, "");
    }

    /**
     * 插入记录
     */
    @ApiOperation("新增配置信息")
    @PostMapping("/insert")
    public ResponseResult<String> insert(@RequestBody RtCfgBomConfigurationDO configurationDO) {
        boolean result = configurationService.insert(configurationDO);
        if (result) {
            return ResponseResult.success("新增成功");
        } else {
            return ResponseResult.fail("","500", "新增失败");
        }
    }

    /**
     * 更新记录
     */
    @ApiOperation("更新配置信息")
    @PutMapping("/update")
    public ResponseResult<String> updateByPrimaryKey(@RequestBody RtCfgBomConfigurationDO configurationDO) {
        boolean result = configurationService.updateByPrimaryKey(configurationDO);
        if (result) {
            return ResponseResult.success("更新成功");
        } else {
            return ResponseResult.fail("","500", "更新失败");
        }
    }

    /**
     * 删除记录
     */
    @ApiOperation("删除配置信息")
    @DeleteMapping("/{oid}")
    public ResponseResult<String> deleteByPrimaryKey(@PathVariable Long oid) {
        boolean result = configurationService.deleteByPrimaryKey(oid);
        if (result) {
            return ResponseResult.success("删除成功");
        } else {
            return ResponseResult.fail("","500", "删除失败");
        }
    }

    /**
     * 返回所有的段号
     */
    @ApiOperation("返回所有的段号")
    @GetMapping("/list/segmNum")
    public ResponseResult<List<String>> listAllSegmNum() {
        List<String> list = configurationService.listAllSegmNum();
        return ResponseResult.success(list,"");
    }
}
